package com.eazykitchen.service;

import com.eazykitchen.dao.CollectDao;
import com.eazykitchen.entity.Collect;
import com.eazykitchen.entity.Paginating;
import com.eazykitchen.entity.UserInformation;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService{

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    CollectDao collectDao;

    @Override
    public int addFood(Collect collect) throws Exception {
        try{
            return collectDao.addFood(collect);
        }catch (Exception e){
            log.error("CollectServiceImpl-->>addFood",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int delFood(int id) throws Exception {
        try {
            return collectDao.delFood(id);
        }catch (Exception e){
            log.error("CollectServiceImpl-->>delFood",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int updateFood(Collect collect) throws Exception {
        int flag = 0;
        try {
            collectDao.updateFood(collect);
            flag = 1;
        } catch (Exception e) {
            log.debug("CollectServiceImpl-->>updateFood", e);
            throw new RuntimeException(e.getMessage());
        }
        return flag;
    }

    @Override
    public Paginating<Collect> getFoodList(Collect collect) throws Exception {
        try {
            PageHelper.startPage(collect.getPageNum(),collect.getPageSize());
            List<Collect> list = collectDao.getFoodList(collect);
            PageInfo<Collect> p = new PageInfo<>(list);
            return new Paginating<>(p);
        }catch (Exception e){
            log.debug("CollectServiceImpl-->>getFoodName",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Collect getFoodById(int id) throws Exception {
        try {
            Collect collect = new Collect();
            return collectDao.getFoodById(id);
        }catch (Exception e){
            log.debug("CollectServiceImpl-->>getFoodById",e);
            throw new RuntimeException(e.getMessage());
        }
    }
}
